package test;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @Author GongXl
 * @Description 用户登录功能
 * @Date 006 15:18
 * @Param
 * @return
**/
@WebServlet(name="userServlet",urlPatterns={"/login"})
public class UserServlet extends HttpServlet{
//    @Override
//    public void init(ServletConfig servletConfig) throws ServletException {
//        ServletContext servletContext = servletConfig.getServletContext();//application
//        //SpringMVC
//        String companyName = servletContext.getInitParameter("companyName");
//        System.out.println("UserServlet coname:"+companyName);
//        System.out.println(servletConfig.getServletName());
//        Enumeration<String> initParameterNames = servletConfig.getInitParameterNames();
//        while (initParameterNames.hasMoreElements()){
//            System.out.println("eeeeee:"+initParameterNames.nextElement());
//        }
//    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");//post
        resp.setContentType("text/html;charset=utf-8");
        ServletOutputStream outputStream = resp.getOutputStream();

        String userName  = req.getParameter("userName");
        String pas = req.getParameter("pas");
        String[] hobbies = req.getParameterValues("hobbies");

//        //所有参数的key
//        Enumeration paramNames = req.getParameterNames();
//        while(paramNames.hasMoreElements())
//        {
//            String paramName = (String)paramNames.nextElement();
//            outputStream.print(paramName + " : " + req.getParameter(paramName) + "<br>");
//            /*如果要考虑同一个请求头名可能出现多次，那么应该用下面的代码段代替上面一行程序代码*/
//	        String [] paramValues = req.getParameterValues(paramName);
//	//良好的编程习惯，在使用对象和数组之前先判断其是否为null
//                if(paramValues != null)
//                {
//                    for(int i=0;i<paramValues.length;i++)
//                    {
//                        outputStream.print(paramName + " : " + (String)paramValues[i] + "<br>");
//                    }
//                }
//        }
//
//        Map<String, String[]> parameterMap = req.getParameterMap();
//        Set<Map.Entry<String, String[]>> entries = parameterMap.entrySet();
//        Iterator<Map.Entry<String, String[]>> iterator = entries.iterator();
//        while (iterator.hasNext()){
//            Map.Entry<String, String[]> entry = iterator.next();
//            String[] ary = entry.getValue();
//            for (String s:ary){
//                System.out.println();
//                outputStream.print("map:"+entry.getKey()+":"+s);
//            }
//            System.out.println("map:"+entry.getKey()+":"+entry.getValue());
//        }
//
//
//
//
//
//        String info;
//        if(null != userName && userName.equals("z3") && pas != null && pas.equals("123")){
//            info = "<span style='color:red'>你好，欢迎"+userName+"登录</span>";
//            for (String s:hobbies){
//                info += ','+s;
//            }
//        }else{
//            info = "<span style='color:red'>抱歉，账号密码不一致</span>";
//        }
        req.setAttribute("k1","k1111111");
//        outputStream.write(info.getBytes());
//        outputStream.flush();
//        outputStream.close();
//        req.getRequestDispatcher("/reg").forward(req,resp);//  服务器跳转  /代表本项目的web目录
        resp.sendRedirect("/docs");//tomcat 的webapps /

        System.out.println("userservlet called end");

    }
}
